package com.moxi.mogublog.xo.service.xuan.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moxi.cloudblog.base.enums.EStatus;
import com.moxi.cloudblog.base.holder.RequestHolder;
import com.moxi.mogublog.commons.entity.Blog;
import com.moxi.mogublog.commons.vo.BlogVO;
import com.moxi.mogublog.utils.ResultUtil;
import com.moxi.mogublog.xo.global.MessageConf;
import com.moxi.mogublog.xo.global.SQLConf;
import com.moxi.mogublog.xo.service.origin.BlogService;
import com.moxi.mogublog.xo.service.xuan.WebBlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


@Service
public class WebBlogServiceImpl implements WebBlogService {


    @Autowired
    private BlogService blogService;


    @Override
    public String addBlog(BlogVO blogVO) {
        Blog blog = new Blog();

        blog.setAuthor(blogVO.getAuthor());
        blog.setArticlesPart(blogVO.getArticlesPart());
        blog.setTitle(blogVO.getTitle());
        blog.setSummary(blogVO.getSummary());
        blog.setContent(blogVO.getContent());
        blog.setTagUid(blogVO.getTagUid());
        blog.setBlogSortUid(blogVO.getBlogSortUid());
        blog.setFileUid(blogVO.getFileUid());
        blog.setIsOriginal(blogVO.getIsOriginal());
        blog.setIsPublish(blogVO.getIsPublish());
        blog.setType(blogVO.getType());
        blog.setOutsideLink(blogVO.getOutsideLink());
        blog.setStatus(EStatus.ENABLE);
        blog.setOpenComment(blogVO.getOpenComment());
        Boolean isSave = blogService.save(blog);

        //保存成功后，需要发送消息到solr 和 redis
        blogService.updateSolrAndRedis(isSave, blog);
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String editBlog(BlogVO blogVO) {
        Blog blog = blogService.getById(blogVO.getUid());
        //如果是原创，作者为用户的昵称
        blog.setAuthor(blogVO.getAuthor());
        blog.setArticlesPart(blogVO.getArticlesPart());
        blog.setTitle(blogVO.getTitle());
        blog.setSummary(blogVO.getSummary());
        blog.setContent(blogVO.getContent());
        blog.setTagUid(blogVO.getTagUid());
        blog.setBlogSortUid(blogVO.getBlogSortUid());
        blog.setFileUid(blogVO.getFileUid());
        blog.setIsOriginal(blogVO.getIsOriginal());
        blog.setIsPublish(blogVO.getIsPublish());
        blog.setOpenComment(blogVO.getOpenComment());
        blog.setUpdateTime(new Date());
        blog.setType(blogVO.getType());
        blog.setOutsideLink(blogVO.getOutsideLink());
        blog.setStatus(EStatus.ENABLE);

        Boolean isSave = blog.updateById();
        //保存成功后，需要发送消息到solr 和 redis
        blogService.updateSolrAndRedis(isSave, blog);
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }
}
